/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.service.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.BankTransactionDao;
import com.boc.botv.dao.UserDao;
import com.boc.botv.model.BankTransaction;
import com.boc.botv.model.PaymentMethod;
import com.boc.botv.model.User;
import com.boc.botv.service.BankTransactionManager;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Fabien Renaud
 */
@Service("bankTransactionManager")
//@Transactional(readOnly = true)
public class BankTransactionManagerImpl implements BankTransactionManager {

    private final Log logger = LogFactory.getLog(BankTransactionManagerImpl.class);
    private BankTransactionDao bankTransactionDao;
    private UserDao userDao;

    @Autowired
    public void setDao(UserDao userDao, BankTransactionDao bankTransactionDao) {
        this.userDao = userDao;
        this.bankTransactionDao = bankTransactionDao;
    }

    public List<BankTransaction> getBankTransactions() {
        logger.info("[BankTransactionManager] Getting bank transactions");
        List<BankTransaction> bankTransactions = bankTransactionDao.getBankTransactions();

        return bankTransactions;
    }

    public List<BankTransaction> getBankTransactions(int userId) {
        logger.info("[BankTransactionManager] Getting bank transaction #" + userId);
        List<BankTransaction> bl = bankTransactionDao.getBankTransactions(userId);

        return bl;
    }

    public List<BankTransaction> getApprovedBankTransactions(int userId) {
        logger.info("[BankTransactionManager] Getting approved bank transactions #" + userId);
        List<BankTransaction> bl = bankTransactionDao.getApprovedBankTransactions(userId);

        return bl;
    }

    public List<BankTransaction> getUnapprovedBankTransactions(int userId) {
        logger.info("[BankTransactionManager] Getting unapproved bank transactions #" + userId);
        List<BankTransaction> bl = bankTransactionDao.getUnapprovedBankTransactions(userId);

        return bl;
    }

    //@Transactional(readOnly = false)
    public boolean approveBankTransaction(int id) {
        if (id < 0) {
            return false;
        }
        logger.info("[BankTransactionManager] Approving bank transactions #" + id);
        
        BankTransaction banktransaction = bankTransactionDao.getBankTransaction(id);
        if (banktransaction == null) {
            return false;
        }
        banktransaction.setIsApproved(true);
        Date now = new Date();
        banktransaction.setTransactionTime(now);

        return true;
    }

    //@Transactional(readOnly = false)
    public boolean debitVirtualAccount(int userId, double amount) {
        if (userId < 1 || amount <= 0) {
            return false;
        }
        logger.info("[BankTransactionManager] Debiting " + amount + " € bank account #" + userId);
        
        User user = this.userDao.getUser(userId);
        if (user == null || user.getPaymentMethods().isEmpty()) {

            return false;
        }
        Date now = new Date();
        PaymentMethod pm = ( (List<PaymentMethod>) user.getPaymentMethods() ).get(0);
        BankTransaction banktransaction = new BankTransaction(pm, amount, 0, now, now, false);
        //BankTransaction banktransaction = new BankTransaction(null, amount, 0, now, now, false);
        //user.getBankTransactionSet().add(banktransaction);
        this.bankTransactionDao.saveBankTransaction(banktransaction);

        return true;
    }

    //@Transactional(readOnly = false)
    public boolean creditVirtualAccount(int userId, double amount) {
        if (userId < 1 || amount <= 0) {
            return false;
        }
        logger.info("[BankTransactionManager] Crediting " + amount + " € bank account #" + userId);
        
        User user = this.userDao.getUser(userId);
        if (user == null || user.getPaymentMethods().isEmpty()) {

            return false;
        }
        Date now = new Date();
        PaymentMethod pm = ( (List<PaymentMethod>) user.getPaymentMethods() ).get(0);
        BankTransaction banktransaction = new BankTransaction(pm, amount, 0, now, now, false);
        //BankTransaction banktransaction = new BankTransaction(null, 0, amount, now, now, false);
        //user.getBankTransactionSet().add(banktransaction);
        this.bankTransactionDao.saveBankTransaction(banktransaction);

        return true;
    }
}
